<template>
    <div class="main">
        <el-card class="box-card">
            <el-form :inline="true" :model="page" class="demo-form-inline">
                <el-form-item label="员工姓名">
                    <el-input v-model="page.name" placeholder="请输入员工姓名" clearable />
                </el-form-item>
                <el-form-item label="是否启用">
                    <el-select v-model="page.status" placeholder="请选择" clearable>
                        <el-option label="是" :value="1" />
                        <el-option label="否" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">搜索</el-button>
                </el-form-item>
            </el-form>
            <el-button type="primary" style="margin-bottom: 30px;" @click="addEdiClick(1)">添加角色</el-button>
            <el-table :data="tableData" style="width: 100%" ref="multipleTableRef" max-height="600px">
                <el-table-column type="index" width="55" />
                <el-table-column prop="name" label="员工姓名" align="center" />
                <el-table-column prop="code" label="code" align="center" />
                <el-table-column prop="remarks" label="简介" align="center" />
                <el-table-column label="状态" align="center">
                    <template #default="scope">
                        <el-switch v-model='scope.row.status'
                            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" active-value="0"
                            inactive-value="1" />
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" align="center" />
                <el-table-column fixed="right" label="操作" width="200">
                    <template #default="scope">
                        <el-button link type="primary" size="small" @click="allocation(scope.row)">分配权限</el-button>
                        <el-button link type="primary" size="small" @click="addEdiClick(2, scope.row)">编辑</el-button>
                        <el-button link type="danger" size="small" @click="delData(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <Pagination v-if="total != 0" :page="page" :total="total" @pageSizeChanged="handlePageSizeChange"
                @currentPageChanged="handleCurrentPageChange" />
        </el-card>
    </div>
    <el-dialog v-model="dialogVisible" :title="isAdd ? '新增' : '修改'" width="500px" :before-close="handleClose">
        <addEdi v-if="dialogVisible" :isAdd="isAdd" @closeAdd="handleClose" :row="rowData.row" @closeOK="closeOK">
        </addEdi>
    </el-dialog>
    <el-dialog v-model="isAllocation" title="分配" width="500px" :before-close="allocationClose">
        <Allocation v-if="isAllocation" :row="rowData.row" @treeOK="treeOK">
        </Allocation>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="allocationClose">取消</el-button>
                <el-button type="primary" @click="OKtree">
                    确认
                </el-button>
            </span>
        </template>
    </el-dialog>

</template>

<script setup>
import { onMounted, reactive, ref } from "vue";
import { roleList, deleteRole, roleListPermission } from '@/api/permission.js'
import Pagination from '@/components/pagination/index.vue' // 引入分页组件
import { ElMessage, ElMessageBox } from 'element-plus'
import addEdi from "./addEdi.vue";
import Allocation from "./allocation.vue";

const dialogVisible = ref(false);
const isAllocation = ref(false);
const isAdd = ref(false);
const page = reactive({
    name: "",
    status: "",
    pageNum: 1,
    pageSize: 10
})
const total = ref(0)
const tableData = reactive([])
const rowData = reactive({
    row: {}
})

const treeList = reactive([])
// 搜索
const onSubmit = () => {
    getList()
}
// 分页
const handlePageSizeChange = (val) => {
    page.pageNum = 1
    page.pageSize = val
    getList()
}

const handleCurrentPageChange = (val) => {
    page.pageNum = val
    // page.pageSize = 10
    getList()
}


// 新增和修改
const addEdiClick = (num, row) => {
    dialogVisible.value = true
    if (num == 2) {
        isAdd.value = false
        rowData.row = row
    } else {
        isAdd.value = true

    }
}
// 确认弹框
const closeOK = () => {
    dialogVisible.value = false;
    getList()
}

// 关闭弹框
const handleClose = () => {
    dialogVisible.value = false;
}

// 删除
const delData = (scope) => {
    ElMessageBox.confirm(
        '是否删除该条数据?',
        '删除',
        {
            confirmButtonText: 'OK',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            deleteRole(scope.id).then(res => {
                if (res.code == 200) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                    getList()
                }
            })

        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            })
        })
}

// 获取列表
const getList = () => {
    tableData.length = 0;
    total.value = 0
    roleList(page).then(res => {
        if (res.code == 200) {
            tableData.push(...res.data.records)
            total.value = res.data.total
        }
    })
}

// 分配数据
const treeOK = (val) => {
    treeList.length = 0
    treeList.push(...val)
}
// 分配权限
const allocation = (scope) => {
    rowData.row = scope;
    isAllocation.value = true;
}

// 分配关闭
const allocationClose = () => {
    isAllocation.value = false;
}
// 分配确认
const OKtree = () => {
    let par = {
        id: rowData.row.id,
        permissionIds: treeList
    }
    roleListPermission(par).then(res => {
        if (res.code == 200) {
            ElMessage({
                type: 'success',
                message: '分配成功',
            })
            isAllocation.value = false;
        }
    })
}


onMounted(() => {
    getList()
})
</script>

<style lang="scss" scoped>
.box-card ::v-deep .el-select__wrapper {
    width: 100px;
}

.main {
    padding: 0 5px 5px 5px;
}
</style>